const Router = require('koa-router');
const router = new Router();
const tool = require('../../tools/tool')
const {db, QueryTypes} = require("../../models/db");

// 应付款汇总
router.post('/report/yfkhz', async ctx => {
    let user = ctx.user
    let body = ctx.request.body
    let formData = body.formData
    let tenantId = user.tenantId;
    let beginDate = formData.beginDate ? formData.beginDate: '2001-01-01';
    let endDate = formData.endDate?formData.endDate:tool.getDay();
    let buId = formData.buId?formData.buId:0;
    let salesId = formData.salesId?formData.salesId:0;
    let searchTxt = formData.searchTxt?formData.searchTxt:'';
    let excel = body.excel
    let sqls = ` exec list_yfhz @tenantId=${tenantId},@buId=${buId},@salesId=${salesId},@beginDate='${beginDate}',@endDate='${endDate}',@type='gp' `

    let list = await db.query(sqls, {type: QueryTypes.SELECT})
    if(excel) {
        return tool.doExcelOut(ctx, list)
    }
    else {
        list.map(row => {
            row.beginDate = beginDate
            row.endDate = endDate
        })
        return ctx.suc('', {
            total: list.length,
            list: list
        })
    }
})



module.exports = router;
